from django.conf import settings
from urllib.parse import urlencode,parse_qs
from urllib.request import urlopen
import json

class QQOauth(object):

    def get_oauth_url(self):
        # 确定请求的url
        base_url = 'https://graph.qq.com/oauth2.0/authorize?'

        state = '/'
        # 确定请求的参数
        params = {
            'response_type': 'code',
            'client_id': settings.QQ_APP_ID,
            'redirect_uri': settings.QQ_REDIRECT_URL,
            'state': state
            # 'scope':'get_user_info'
        }
        # 将参数拼接到url中
        # 需要将字典遍历
        from urllib.parse import urlencode
        auth_url = base_url + urlencode(params)

        return auth_url

    def get_access_token(self,code):
        # 2.确定base_url
        base_url = 'https://graph.qq.com/oauth2.0/token?'
        # 3.拼接参数
        params = {
            'grant_type': 'authorization_code',
            'client_id': settings.QQ_APP_ID,
            'client_secret': settings.QQ_APP_KEY,
            'code': code,
            'redirect_uri': settings.QQ_REDIRECT_URL
        }
        # 4.生成url
        token_url = base_url + urlencode(params)
        # 5.发送请求,获取返回数据
        response = urlopen(token_url)
        data = response.read().decode()

        res_params = parse_qs(data)

        # print(res_params)
        access_token = res_params['access_token']
        # print(access_token)

        access_token = access_token[0]
        return access_token


    def get_openid(self,access_token):

        # https://graph.qq.com/oauth2.0/me
        # GET
        # access_token        必须      在Step1中获取到的accesstoken。
        # 返回数据PC网站接入时，获取到用户OpenID，返回包如下：
        # callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );
        # openid是此网站上唯一对应用户身份的标识，网站可将此ID进行存储便于用户下次登录时辨识其身份，
        # 或将其与用户在网站上的原有账号进行绑定

        base_url = 'https://graph.qq.com/oauth2.0/me?'
        params = {
            'access_token':access_token
        }
        openidurl = base_url + urlencode(params)

        #请求url的数据
        response = urlopen(openidurl)
        #获取url的数据
        data = response.read().decode()
        print(data)

        try:
            dict = json.loads(data[10:-4])
        except Exception:
            raise Exception('数据读取错误')

        print(dict)

        return dict.get('openid', None)